package com.lzp.controller;

import com.lzp.pojo.Carousel;
import com.lzp.pojo.Category;
import com.lzp.pojo.vo.CategoryVO;
import com.lzp.pojo.vo.NewItemsVO;
import com.lzp.service.CarouselService;
import com.lzp.service.CategoryService;
import com.lzp.utils.JsonUtils;
import com.lzp.utils.RedisOperator;
import com.lzp.utils.ServerResponse;
import com.lzp.utils.enums.Yes0rNo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页展示相关接口
 *
 * @author dtyy
 * @date 2020/8/2
 */
@Api(value = "首页展示", tags = "首页展示相关接口")
@RestController
@RequestMapping("index")
public class IndexController {

    @Autowired
    private CarouselService carouselService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisOperator redis;

    /**
     * 缓存更新时机
     * 1. 后台更新，更新缓存
     * 2. 定时刷新缓存
     * 3. 缓存的内容如果设置了超时时间，超时再重置。
     *
     * @return
     */
    @ApiOperation(value = "获取首页轮播图列表", notes = "获取首页轮播图列表", httpMethod = "GET")
    @GetMapping("/carousel")
    public ServerResponse carousel() {
        List<Carousel> list = new ArrayList<>();
        String cache = redis.get("foodie:carousel");
        if (StringUtils.isBlank(cache)) {
            list = carouselService.listCarousels(Yes0rNo.YES.getCode());
            redis.set("foodie:carousel", JsonUtils.objectToJson(list), 600);
        } else {
            list = JsonUtils.jsonToList(cache, Carousel.class);
        }
        return ServerResponse.createBySuccess(list);
    }

    @ApiOperation(value = "获取商品分类(一级分类)", notes = "获取商品分类(一级分类)", httpMethod = "GET")
    @GetMapping("/cats")
    public ServerResponse cats() {
        List<Category> list = new ArrayList<>();
        String cache = redis.get("foodie:cats");
        if (StringUtils.isBlank(cache)) {
            list = categoryService.listRootCategories();
            redis.set("foodie:cats", JsonUtils.objectToJson(list), 600);
        } else {
            list = JsonUtils.jsonToList(cache, Category.class);
        }
        return ServerResponse.createBySuccess(list);
    }

    /**
     * 前端懒加载一级分类查看二三级分类
     * <p>
     * /subCat/{rootCatId}
     *
     * @param rootCatId
     * @return
     */
    @ApiOperation(value = "获取商品子分类", notes = "获取商品子分类", httpMethod = "GET")
    @GetMapping("/subCat/{rootCatId}")
    public ServerResponse subCat(
            @ApiParam(name = "rootCatId", value = "一级分类id", required = true)
            @PathVariable Integer rootCatId) {
        if (rootCatId == null) {
            ServerResponse.createByErrorMsg("商品分类不存在");
        }
        List<CategoryVO> list = new ArrayList<>();
        String cache = redis.get("foodie:subCat:" + rootCatId);
        if (StringUtils.isBlank(cache)) {
            list = categoryService.listSubCategories(rootCatId);
            if (list != null && !list.isEmpty()) {
                redis.set("foodie:subCat:" + rootCatId, JsonUtils.objectToJson(list));
            } else {
                redis.set("foodie:subcat:" + rootCatId, JsonUtils.objectToJson(list), 300);
            }
        } else {
            list = JsonUtils.jsonToList(cache, CategoryVO.class);
        }
        return ServerResponse.createBySuccess(list);
    }

    /**
     * 前端懒加载一级分类专栏
     * <p>
     * /sixNewItems/{rootCatId}
     *
     * @param rootCatId
     * @return
     */
    @ApiOperation(value = "查询每个一级分类下的最新6条商品数据", notes = "查询每个一级分类下的最新6条商品数据", httpMethod = "GET")
    @GetMapping("/sixNewItems/{rootCatId}")
    public ServerResponse sixNewItems(
            @ApiParam(name = "rootCatId", value = "一级分类id", required = true)
            @PathVariable Integer rootCatId) {
        if (rootCatId == null) {
            ServerResponse.createByErrorMsg("商品分类不存在");
        }
        List<NewItemsVO> list = categoryService.listSixNewItems(rootCatId);
        return ServerResponse.createBySuccess(list);
    }

}
